package com.va.yahooweather.entity;

import com.va.yahooweather.commons.units.*;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

public class YUnit {
    
    private TempratureUnit temperature;
    private DistanceUnit distance;
    private PressureUnit pressure;
    private SpeedUnit speed;
    
    public YUnit() {
        temperature = TempratureUnit.C;
        distance = DistanceUnit.km;
        pressure = PressureUnit.in;
        speed = SpeedUnit.kph;
    }

    public TempratureUnit getTemperature() {
        return temperature;
    }

    public void setTemperature(TempratureUnit temperature) {
        this.temperature = temperature;
    }
    
    public void setTemperature(String temperature) {
        char c = temperature.charAt(0);
        if (c == 'c' || c == 'C') {
            this.temperature = TempratureUnit.C;
            return;
        }
        
        this.temperature = TempratureUnit.F;
        
    }

    public DistanceUnit getDistance() {
        return distance;
    }

    public void setDistance(DistanceUnit distance) {
        this.distance = distance;
    }
    
    public void setDistance(String distance) {
        char c = distance.charAt(0);
        if (c == 'k' || c == 'K') {
            this.distance = DistanceUnit.km;
            return;
        }
        
        this.distance = DistanceUnit.mi;
    }

    public PressureUnit getPressure() {
        return pressure;
    }

    public void setPressure(PressureUnit pressure) {
        this.pressure = pressure;
    }

    public void setPressure(String pressure) {
        char c = pressure.charAt(0);
        if (c == 'i' || c == 'I') {
            this.pressure = PressureUnit.in;
            return;
        }
        
        this.pressure = PressureUnit.mb;
    }

    public SpeedUnit getSpeed() {
        return speed;
    }

    public void setSpeed(SpeedUnit speed) {
        this.speed = speed;
    }
    
    public void setSpeed(String speed) {
        char c = speed.charAt(0);
        if (c == 'k' || c == 'K') {
            this.speed = SpeedUnit.kph;
            return;
        }
        
        this.speed = SpeedUnit.mph;
    }
    
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        
        if (this == obj) {
            return true;
        }
        
        YUnit target = (YUnit) obj;
        return new EqualsBuilder()
            .append(getDistance(), target.getDistance())
            .append(getPressure(), target.getPressure())
            .append(getSpeed(), target.getSpeed())
            .append(getTemperature(), target.getTemperature())
            .isEquals();
    }

    @Override
    public int hashCode() {
        HashCodeBuilder h = new HashCodeBuilder(5, 29);
        h.append(getDistance());
        h.append(getPressure());
        h.append(getSpeed());
        h.append(getTemperature());
        return h.hashCode();
    }
}
